﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Light.Framework.Contract;
using System.Web;

namespace Light.Framework.Web
{
    class BaseListPage<T, ITComponent> : BasePage<T, ITComponent>
        where T : class, IEntity, new()
        where ITComponent : IComponent<T>
    {

        const string DETAIL = "detail";
        const string DELETE = "delete";
        const string ITEM_ID = "uid";

        private T _model = null;

        /// <summary>
        /// 当前实体
        /// </summary>
        public virtual T Model
        {
            get
            {
                if (_model == null)
                {
                    _model = new T();
                }
                return _model;
            }
            set
            {

                _model = value;
            }
        }

        /// <summary>
        /// 删除请求
        /// </summary>
        protected virtual bool IsDeleteRequest
        {

            get
            {
                return !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[DELETE]);
            }
        }

        /// <summary>
        /// 获取详细请求
        /// </summary>
        protected virtual bool IsDetailRequest
        {

            get
            {
                return !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[DETAIL]);
            }
        }

        /// <summary>
        /// 新增请求
        /// </summary>
        protected virtual bool IsCreateRequest
        {
            get
            {
                string uid = this.Request.QueryString[ITEM_ID];

                if (String.IsNullOrEmpty(uid))
                    return false;
                else
                    return Convert.ToInt32(uid) <= 0;
            }
        }

        /// <summary>
        /// 更新请求
        /// </summary>
        protected virtual bool IsUpdateRequest
        {
            get
            {
                string uid = this.Request.QueryString[ITEM_ID];

                if (String.IsNullOrEmpty(uid))
                    return false;
                else
                    return Convert.ToInt32(uid) > 0;
            }
        }


        protected bool IsRequest(string queryString)
        {
            return !String.IsNullOrEmpty(Request.QueryString[queryString]);
        }
    }
}
